package com.cancan.cloud.shop.controller;

import com.cancan.cloud.shop.domain.Order;
import com.cancan.cloud.shop.domain.Product;
import com.cancan.cloud.shop.feign.RpcService;
import com.cancan.cloud.shop.service.impl.OrderServiceImpl;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: cancan-java-share
 * @description: 定单控制器
 * @author: czchen
 * @date: 2023-02-01 14:40:53
 */
@RestController
@Slf4j
public class OrderController {

    @Autowired
    private OrderServiceImpl orderService;

//    @Autowired
//    private ProductFeignApi productFeignApi;

    @Autowired
    private RpcService rpcService;

    @RequestMapping("/test")
    public String test() {
        orderService.save(1L,88888L);
        return "执行成功";
    }

    @RequestMapping("/aaa")
    public String aaa() {
        orderService.save(1L,88888L);
        return "执行成功";
    }

    @RequestMapping("/test2")
    public String test2() {
//        Product product = productFeignApi.find(2222L);
//        System.out.println(product);
        return "执行成功";
    }

    @RequestMapping("/test3")
    @HystrixCommand(fallbackMethod = "failSave")
    public Order save3(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId, HttpServletRequest request){
        log.info("订单服务:  接受的请求参数:userId:{},productId:{}",userId,productId);
        log.info("正常业务处理");
        //接受请求头信息里面的两个参数 token 和 cookie
        String token = request.getHeader("token");
        String cookie = request.getHeader("cookie");

        log.info("接受的头信息: token:{},cookie:{}",token,cookie);
        Order order = orderService.save(userId, productId);

        if(userId%3==0){
            int i=1/0;
        }
        log.info("订单服务: 响应的数据:{}",order.toString());
        return order;
    }

    public Order failSave(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId, HttpServletRequest request){
        log.info("执行降级处理的方案");
        new Thread(()->{
            //赶紧发短信给运维人员  如果出了问题, 每隔20分分钟发送一次短信,知道问题解决
            //从redis中查找对应的数据
            //Boolean flag = redisTemplate.hasKey("order-server");
            Boolean flag = true;
            if(!flag){
                log.info("运维叔叔, 请赶紧处理订单系统业务问题");
                //redisTemplate.opsForValue().set("order-server","true",10, TimeUnit.SECONDS);
            }else{
                log.info("已经发送过短信了");
            }
        }).start();
        Order order = new Order();
        return order;
    }

    @RequestMapping("/test4")
    public String test4() {
        Product query = rpcService.query(7777L);
        System.out.println(query);
        return "执行成功";
    }
}
